/*---------------------------------------------------------------
 * Confidential and Proprietary
 * Copyright 2006 CMO & Hewlett-Packard Development Company, L.P.
 * All Rights Reserved
 *---------------------------------------------------------------
 * Project Name    : SPF
 * Sub Project Name: SPF
 *
 * Class Name      : ExceptionUtil
 * Purpose         : 
 *---------------------------------------------------------------
 * Modification Log
 * Date         Ver. #      Programmer       Description
 *
 * Apr 6, 2006    1.0          qixia          Initial
 *---------------------------------------------------------------
 */
package com.erdos.common.util;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Locale;


public class ExceptionUtil {

    private static HashMap helperMap=new HashMap();
	public static Throwable getRootException(Throwable e){
		Throwable root=e;
		while(root.getCause()!=null)root=root.getCause();
		return root;
	}
	
	public static String getHtmlStackTrackInfo(Throwable e) {
		return StringUtil.htmlEncode(getStackTrackInfo(e));
	}
	
	public static String getStackTrackInfo(Throwable e) {
		StringBuffer sb = new StringBuffer();
		StackTraceElement[] ste = e.getStackTrace();
		sb.append(e.getClass().getName()+":"+e.getMessage()+"\n");
		for (int i = 0; i < ste.length; i++) {
			sb.append("    at ");
			sb.append(ste[i].toString());
			sb.append("\n");
		}
		while (e.getCause()!=null){
			e=e.getCause();
			ste=e.getStackTrace();
			sb.append("Caused by:"+e.getClass().getName()+":"+e.getMessage()+"\n");
			for (int i = 0; i < ste.length; i++) {
				sb.append("    at ");
				sb.append(ste[i].toString());
				sb.append("\n");
			}
		}
		return sb.toString();
	}
    
//    public static SQLExceptionHelper getSQLExceptionHelper(Locale locale) {
//        return getSQLExceptionHelper(SystemParams.DEFAULT_DB_HELPER,locale);
//    }

    public static SQLExceptionHelper getSQLExceptionHelper(String dbHelperName,Locale locale) {
        if (helperMap.containsKey(dbHelperName+locale)) {
            return (SQLExceptionHelper) helperMap.get(dbHelperName+locale);
        }
        Class[] claz= {Locale.class};
        Object[] objs={locale};
        SQLExceptionHelper helper = null;
		try {
			helper = (SQLExceptionHelper)Class.forName(dbHelperName).
			        getConstructor(claz).newInstance(objs);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
            throw new RuntimeException(e);
		} catch (SecurityException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
		} catch (InstantiationException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
		} catch (IllegalAccessException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
		} catch (InvocationTargetException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
		} catch (NoSuchMethodException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
		} catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
		}
		helperMap.put(dbHelperName+locale,helper);
		return helper;
    }
}

